/*
 *    Copyright (c) 2018-2025, proj All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the proj developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: proj
 */
package com.ykht.proj.coupon.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ykht.proj.common.security.util.SecurityUtils;
import com.ykht.proj.coupon.Enum.ShareEnum;
import com.ykht.proj.coupon.entity.*;
import com.ykht.proj.coupon.mapper.FxCmsInfoMapper;
import com.ykht.proj.coupon.mapper.FxShareMapper;
import com.ykht.proj.coupon.service.FxConfBrandPersonService;
import com.ykht.proj.coupon.service.FxShareService;
import com.ykht.proj.coupon.service.FxShareSingleService;
import org.apache.poi.poifs.filesystem.POIFSStream;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

/**
 * 普通分享资讯表
 *
 * @author proj code generator
 * @date 2021-07-07 16:20:00
 */
@Service
@SuppressWarnings("all")
public class FxShareServiceImpl extends ServiceImpl<FxShareMapper, FxShare> implements FxShareService {


	@Resource
	private FxCmsInfoMapper fxCmsInfoMapper;
	@Resource
	private FxShareSingleService fxShareSingleService;
	@Resource
	private FxConfBrandPersonService fxConfBrandPersonService;

	@Transactional(rollbackFor = Exception.class)
	@Override
	public FxShare share(FxShare fxShare) {
		Long loginId = SecurityUtils.getPersonId();
		FxConfBrandPerson enableConf = fxConfBrandPersonService.findEnableConf(fxShare.getBrandId());
		if (enableConf != null) {
			fxShare.setConfBrandPersonId(enableConf.getId());
		}
		//判断 是否分享过
		FxShareSingle single = saveShareSingle(fxShare);
		fxShare.setSingleId(single.getId());
		//添加分享记录
		fxShare = saveShare(fxShare);
		//0品牌1门店2资讯3海报4商品
		if (ShareEnum.cms.getValue() == fxShare.getType()){
			fxCmsInfoMapper.updateStatisticsByCmsId(fxShare.getTatId(),0,1,0,0,0);
		}
		return fxShare;
	}

	private FxShareSingle saveShareSingle(FxShare share){
		Long loginId = SecurityUtils.getPersonId();
		FxShareSingle single = fxShareSingleService.getOne(Wrappers.lambdaQuery(FxShareSingle.class)
				.eq(FxShareSingle::getPerId,loginId).eq(FxShareSingle::getTatId,share.getTatId()).eq(FxShareSingle::getType,share.getType()));
		if (single == null) {
			single = new FxShareSingle();
			single.setTatId(share.getTatId());
			single.setPerId(loginId);
			single.setType(share.getType());
			single.setCreateUser(loginId);
			single.setCreateTime(LocalDateTime.now());
		}
		single.setLastTime(LocalDateTime.now());
		fxShareSingleService.saveOrUpdate(single);
		return single;
	}
	private FxShare saveShare(FxShare fxShare){
		List<Integer> type = Arrays.asList(0,1,2,3);//0品牌1门店2资讯3海报4商品
		fxShare.setShopType(type.contains(fxShare.getType())?1:2);//1 优惠卷 2 卡
		fxShare.setPerId(SecurityUtils.getPersonId());
		fxShare.setCreateUser(SecurityUtils.getPersonId());
		fxShare.setCreateTime(LocalDateTime.now());
		switch (ShareEnum.getThisByVal(fxShare.getType())){
			case brand:
				CnBrand brand = new CnBrand().selectById(fxShare.getTatId());
				Optional.of(brand).ifPresent(o->{
					fxShare.setCompanyId(brand.getCompanyId());
					fxShare.setBrandId(brand.getId());
				});
				break;
			case shop:
				CnShop shop = new CnShop().selectById(fxShare.getTatId());
				Optional.of(shop).ifPresent(o->{
					fxShare.setCompanyId(shop.getCompanyId());
					fxShare.setBrandId(shop.getBrandId());
					fxShare.setShopId(shop.getId());
				});
				break;
			case cms:
				FxCmsInfo cms = new FxCmsInfo().selectById(fxShare.getTatId());
				Optional.of(cms).ifPresent(o->{
					fxShare.setCompanyId(cms.getCompanyId());
					fxShare.setBrandId(cms.getBrandId());
				});
				break;
			case poster:
				FxConfPosterBrand poster = new FxConfPosterBrand().selectById(fxShare.getTatId());
				Optional.of(poster).ifPresent(o->{
					fxShare.setCompanyId(poster.getCompanyId());
					fxShare.setBrandId(poster.getBrandId());
				});
				break;
			case card:
				break;
		}
		fxShare.insert();
		return fxShare;
	}
}
